package com.augurit.common.sysmeta.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.base.extmodel.BasicCombo;
import com.augurit.common.base.extmodel.base.BaseTreeNode;
import com.augurit.common.sysmeta.service.ISysMetaTableService;
import com.augurit.common.sysmeta.web.form.SysMetaTableForm;
import com.augurit.common.util.ExtUtils;
import com.augurit.wf.mgmt.convert.ConvertToExtModel;

/**
 * 表结构元数据定义Action类
 *
 */

@Results({
	@Result(name = ExtCrudActionSupport.RELOAD, location = "sys-meta-table.action", type = "redirect"),
	@Result(name = ExtCrudActionSupport.SUCCESS, location = "/common/sysmeta/sys-meta.jsp")
})
public class SysMetaTableAction extends ExtCrudActionSupport<SysMetaTableForm>{

	private static final long serialVersionUID = -8432803538560009863L;

	@Autowired
	private ISysMetaTableService sysMetaTableService;
	
	private SysMetaTableForm form;
	private Long id;
	private Long []ids;
	private String nodeId;
	
	private String busEntityName;
	
	// 基本属性
	private Page<SysMetaTableForm> page = new Page<SysMetaTableForm>(15);
	
	
	@Override
	public String delete() throws Exception {
		if(id != null){
			sysMetaTableService.delete(id);
			this.extRenderSuccess();
		}
		return null;
	}
	
	/**
	 * 进入jsp页面
	 * @return
	 */
	public String loadJsp(){
		return SUCCESS;
	}
	
	public String deleteMore() throws Exception {
		if(ids != null && ids.length>0){
			sysMetaTableService.delete(ids);
			this.extRenderSuccess();
		}
		return null;
	}

	@Override
	public String list() throws Exception {
		if(form != null){
			ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
			page = sysMetaTableService.search(form, page);
			this.extRenderGridJson(page);
		}
		return SUCCESS;
	}
	
	
	/**
	 * 获取系统中所有的实体表
	 */
	public String getEntityTables()throws Exception {
		List<BasicCombo> nodes = null;
		
		List<SysMetaTableForm> list =sysMetaTableService.getAllMetaTables();
		nodes = ConvertToExtModel.convertToMetaTableSelectBox(list);
		ExtBaseAction.extRenderListJson(nodes, true);
		return null;
	}
	
	public SysMetaTableForm getModel() {
		return form;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id != null && id != -1L)
			form = sysMetaTableService.get(id);
		else
			form = new SysMetaTableForm();
		
	}

	@Override
	public String save() throws Exception {
		if(form != null){
			sysMetaTableService.save(form);
			this.extRenderSuccess();
		}
		return null;
	}

	public String input()throws Exception{
		if(form != null){
			this.extRenderFormJson(form);
		}
		return null;
	}
	
	/**
	 * 表和字段的树
	 * @return
	 */
	public String viewTree(){
		List<? extends BaseTreeNode> list = null;
		if(nodeId != null){
			list = sysMetaTableService.viewTree(nodeId);
			this.extRenderListJson(list, false);
		}
		return null;
	}

	
	
	//属性的getter和setter方法
	public SysMetaTableForm getForm() {
		return form;
	}

	public void setForm(SysMetaTableForm form) {
		this.form = form;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public String getNodeId() {
		return nodeId;
	}

	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}

	public String getBusEntityName() {
		return busEntityName;
	}

	public void setBusEntityName(String busEntityName) {
		this.busEntityName = busEntityName;
	}


}
